const organization = this.pageObj.getApiWidgetByAliasName('org_tree');
const three = this.pageObj.getApiWidgetByAliasName('三维组件');
const search_input = this.pageObj.getApiWidgetByAliasName('search_input');
const prefix = this.pageObj.getVariables().find(v => v.name === 'environmentType')?.value || '';
const [ select ] = $event;
const value = select.value;
const artemis = three.widgetObj.artemis;

// console.log($event);
// console.log(organization.widgetObj._option.dataSource)
function basePost(url, body) {
    const options = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(body),
    };
    return fetch(url, options).then(res => res.json())
}

function breakDataSource(item) {
    if (item.personId) {
        item.title = item.accountName || "默认name";
        item.userId = item.account;
        item.treeId = item.id;
    } else if(item.orgId || item.orgName) {
        item.title = item.orgName;
        item.id = item.orgId;
        item.treeId = item.orgId;
    }

    item.type = '1' // 叶子节点标识
    if (item.children) {
        item.children = item.children.filter(function (item) { return !item.id }).map(breakDataSource);
        item.type = '0'
    }
    if (item.values) {
        item.values.forEach(function (member) {
             item.children.push(breakDataSource(member));
        });
        item.type = '0'
    }

    return item
}


const accountName = $(search_input.__box).find('.input-filter').val();
basePost(`/${prefix}Communication/classroom/selectAllStructure`, { accountName })
    .then(({ result, success }) => {
        console.log(result, success)
        if (success === true) {
            const dataSource = result.map(breakDataSource)

            if (value === 'online') {
                organization.widgetObj.option_data = artemis.filterUsersOnline(dataSource);
            } else {
                organization.widgetObj.option_data = artemis.filterUsersOnline(dataSource, true);
            }

            organization.widgetObj.render()
            console.log(dataSource)
        }
    })